package cn.petplatform.petback.controller.user;

import cn.petplatform.annotations.LoginVerifyToken;
import cn.petplatform.annotations.PassToken;
import cn.petplatform.dto.ResponseDto;
import cn.petplatform.petback.pojo.User;
import cn.petplatform.petback.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * @author LNL
 * @date 2021/5/31 14:24
 */
@RestController
@Api(tags = "用户模块")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("注册")
    @PostMapping("/register")
    @PassToken
    public ResponseDto register(MultipartHttpServletRequest request) {
        ResponseDto responseDto = userService.register(request);
        return responseDto;
    }
    @PassToken
    @PostMapping("/login_pwd")
    @ApiOperation("普通用户登录")
    public ResponseDto login(@RequestBody User user) {
        ResponseDto responseDto = userService.login(user.getUsername(),user.getPassword());
        return responseDto;
    }

    @PassToken
    @PostMapping("/loginAdmin")
    @ApiOperation("管理员登录")
    public ResponseDto loginAdmin(@RequestBody User user) {
        ResponseDto responseDto = userService.loginAdmin(user.getUsername(),user.getPassword());
        return responseDto;
    }

    @LoginVerifyToken
    @PostMapping("/update")
    @ApiOperation("修改个人信息")
    public ResponseDto updateInfo(@RequestBody User user, HttpServletRequest request) {
        ResponseDto responseDto = userService.updateInfo(user,request);
        return responseDto;
    }

    @LoginVerifyToken
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除用户")
    public ResponseDto deleteUser(@ApiParam("用户id") @PathVariable("id") String id,
                                  HttpServletRequest request) {
        ResponseDto responseDto = userService.deleteUser(id,request);
        return responseDto;
    }

    @PassToken
    @GetMapping("/user/{id}")
    @ApiOperation("获取用户个人信息")
    public ResponseDto getUserInfo(@ApiParam("用户id") @PathVariable("id") String id,
                                  HttpServletRequest request) {
        ResponseDto responseDto = userService.getUserInfo(id,request);
        return responseDto;
    }



}
